<template>
  <div class="app-container">
    <div class="head-container">
      <el-card shadow="never">
        <div class="search-bar">
          <el-button
            v-if="checkPermission(['ADMIN', 'site-config::add'])"
            type="danger"
            @click="addSiteConfig"
            >新增配置</el-button
          >
        </div>
      </el-card>
    </div>

    <el-card shadow="never">
      <div class="data-list-wrap">
        <el-table
          v-loading="loading"
          :data="list"
          size="small"
          style="width: 100%"
        >
          <el-table-column prop="name" label="网站名称"> </el-table-column>

          <el-table-column label="网站备案信息">
            <template slot-scope="scope">
              <div>
                {{ scope.row.recordInformation }}
              </div>
            </template>
          </el-table-column>

          <el-table-column label="网站微信二维码">
            <template slot-scope="scope">
              <el-image
                class="images-body"
                :src="scope.row.wechatQrCode"
                :preview-src-list="[scope.row.wechatQrCode]"
                v-if="scope.row.wechatQrCode"
              ></el-image>
            </template>
          </el-table-column>

          <el-table-column label="网站名字字母代号">
            <template slot-scope="scope">
              <div>
                {{ scope.row.letterCode }}
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <div>
                <el-button
                  @click="updateSiteConfig(scope.row)"
                  type="text"
                  v-if="checkPermission(['ADMIN', 'site-config::update'])"
                  >修改</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>

        <el-dialog
          title="配置信息"
          :visible.sync="addSiteConfigVisible"
          :close-on-click-modal="false"
          :append-to-body="true"
        >
          <el-form :model="form" ref="form" label-width="150px" :rules="rules">
            <el-form-item prop="name" label="网站名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item prop="recordInformation" label="网站备案信息">
              <el-input v-model="form.recordInformation"></el-input>
            </el-form-item>

            <el-form-item prop="wechatQrCode" label="网站微信二维码">
              <div class="businessLicense-content">
                <el-upload
                  action="/api/admin/image"
                  list-type="picture-card"
                  :class="{ disabled: uploadDisabled }"
                  :limit="1"
                  ref="upload"
                  :on-success="handleAvatarSuccess1"
                  :before-upload="beforeAvatarUpload"
                  :on-remove="handleRemove1"
                  :on-preview="handlePictureCardPreview1"
                  :file-list="wechatQrCode"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible"  :close-on-click-modal="false"  :modal-append-to-body="false"
                append-to-body  >
                  <img width="100%" :src="wechatQrCodeUrl" alt="" />
                </el-dialog>
              </div>
            </el-form-item>

            <el-form-item prop="letterCode" label="网站名字字母代号">
              <el-input v-model="form.letterCode"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addSiteConfigVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm" :loading="opLoading"
              >立即新增</el-button
            >
          </div>
        </el-dialog>

        <el-dialog
          title="修改"
          :visible.sync="updateSiteConfigVisible"
          :close-on-click-modal="false"
          :append-to-body="true"
        >
          <el-form :model="form" ref="form" label-width="150px" :rules="rules">
            <el-form-item prop="name" label="网站名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item prop="recordInformation" label="网站备案信息">
              <el-input v-model="form.recordInformation"></el-input>
            </el-form-item>

            <el-form-item prop="wechatQrCode" label="网站微信二维码">
              <div class="businessLicense-content">
                <el-upload
                  action="/api/admin/image"
                  list-type="picture-card"
                  :class="{ disabled: uploadDisabled }"
                  :limit="1"
                  ref="upload"
                  :on-success="handleAvatarSuccess2"
                  :before-upload="beforeAvatarUpload"
                  :on-remove="handleRemove2"
                  :on-preview="handlePictureCardPreview2"
                  :file-list="wechatQrCode"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible"  :close-on-click-modal="false"  :modal-append-to-body="false"
                append-to-body  >
                  <img width="100%" :src="wechatQrCodeUrl" alt="" />
                </el-dialog>
              </div>
            </el-form-item>

            <el-form-item prop="letterCode" label="网站名字字母代号">
              <el-input v-model="form.letterCode"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="updateSiteConfigVisible = false">取消</el-button>
            <el-button type="primary" @click="modifyForm" :loading="opLoading"
              >确定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>


<script>
import { getSiteConfig } from "@/api/business";
import checkPermission from "@/utils/permission";
import Axios from "axios";
import { addSiteConfig } from "@/api/business";
import { updateSiteConfig } from "@/api/business";
const defaultModel = {
  mobile: null,
  username: null,
  pageNo: 1,
  pageSize: 10,
  count: 0,
};
export default {
  data() {
    return {
      uploadBoolean: false,
      uploadDisabled: false,
      loading: false,
      opLoading: false,
      list: [],
      addSiteConfigVisible: false,
      updateSiteConfigVisible: false,
      form: {
        name: "",
        recordInformation: "",
        wechatQrCode: "",
        letterCode: "",
      },
      wechatQrCode: [
        {
          url: "",
        },
      ],
      rules: {
        name: [{ required: true, message: "请输入网站名称", trigger: "blur" }],
      },
      wechatQrCodeUrl:"",
      dialogVisible:false,
    };
  },
  components: {},
  methods: {
    checkPermission,

    addSiteConfig() {
      this.addSiteConfigVisible = true;
    },

    //二维码上传
    handleAvatarSuccess1(res, file) {
      this.form.wechatQrCode = res.data;
      this.wechatQrCode[0] = this.form.wechatQrCode;
    },
    handleAvatarSuccess2(res, file) {
      this.form.wechatQrCode = res.data;
      this.wechatQrCode[0] = this.form.wechatQrCode;
    },
    handleRemove1(file, fileList) {
      this.form.wechatQrCode = "";
    },
    handleRemove2(file, fileList) {
      this.form.wechatQrCode = "";
    },
    handlePictureCardPreview1(file) {
      this.wechatQrCodeUrl = file.url;
      this.dialogVisible = true;
    },
    handlePictureCardPreview2(file) {
      this.wechatQrCodeUrl = file.url;
      this.dialogVisible = true;
    },
    handleofficeImageRemove(file) {
      let url = file.url;
      if (file.response) {
        url = file.response.data;
      }
    },
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isLt5M) {
        this.$message.error("上传大小不能超过 5MB!");
      }
      return isLt5M;
    },
    updateSiteConfig(row) {
      this.updateSiteConfigVisible = true;
      this.form = _.cloneDeep(row);
    },
    modifyForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.opLoading = true;
          updateSiteConfig(this.form).then((data) => {
            if (data.success) {
              this.$notify({
                title: "修改成功",
                type: "success",
                duration: 2500,
              });
              this.init();
            }
            this.opLoading = false;
            this.updateSiteConfigVisible = false;
          });
        } else {
          return false;
        }
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.opLoading = true;
          addSiteConfig(this.form).then((data) => {
            if (data.success) {
              this.$notify({
                title: "新增成功",
                type: "success",
                duration: 2500,
              });
              this.init();
            }
            this.opLoading = false;
            this.addSiteConfigVisible = false;
          });
        } else {
          return false;
        }
      });
    },

    init() {
      getSiteConfig()
        .then((data) => {
          if (data.success) {
            if (data.data) {
              this.list = [data.data];

              this.wechatQrCode[0].url = [data.data.wechatQrCode];
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="scss">
.deleteImg {
  font-size: 30px;
  position: absolute;
  color: #909399;
  top: 50%;
  left: 0;
  right: 0;

  z-index: 999;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 246px;
  height: 138px;
  line-height: 138px;
  background-color: #fff;
  text-align: center;
}
.avatar {
  width: 246px;
  height: 138px;
  display: block;
}

.avatar-uploader1 .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader1 .el-upload:hover {
  border-color: #409eff;
}
</style>
<style lang="scss" scoped>
.businessLicense-content {
  font-size: 12px;
  color: #909399;
  display: flex;
  flex-direction: column;
}
.images-body {
  width: 56px;
  height: 56px;
  border-radius: 8px;
}
.el-upload-list__item-actions:hover {
  opacity: 1;
}
.el-upload-list__item-actions {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  cursor: default;
  text-align: center;
  color: #fff;
  opacity: 0;
  font-size: 20px;
  background-color: rgba(0, 0, 0, 0.5);
  transition: opacity 0.3s;
}
.avatar-uploader-delete {
  font-size: 28px;
  color: #fff;
  width: 246px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
</style>
